package com.book.backend.main;

import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import reactor.core.publisher.Flux;

import java.time.Duration;
import java.util.concurrent.TimeUnit;
import java.util.stream.LongStream;

public class test {
    public static void main(String[] args) {
        // 创建一个Flux流，这里假设我们发送数字序列作为示例
        String content ="以下是一些值得一游的景点和活动推荐：\n\n1. 大鹏所城：这是";
        if (content.contains("\n\n")){
            String replacedStr = content.replaceAll("\\n\\n", "<br><br>");
            System.out.println(replacedStr);
        }
    }
    public static SseEmitter stream() {
        SseEmitter sseEmitter = new SseEmitter(Long.MAX_VALUE);

        sseEmitter.onCompletion(() -> System.out.println("Stream completed"));
        sseEmitter.onTimeout(() -> sseEmitter.complete());
        sseEmitter.onError(ex -> sseEmitter.completeWithError(ex));

        // 模拟数据流，实际情况应替换为从通义千问API获取的流式数据
        new Thread(() -> {
            try {
                for (int i = 0; i < 10; i++) {
                    TimeUnit.SECONDS.sleep(1); // 模拟延迟
                    sseEmitter.send("data: " + i);
                }
                sseEmitter.complete();
            } catch (Exception e) {
                sseEmitter.completeWithError(e);
            }
        }).start();

        return sseEmitter;
    }
}
